var adds={};
Page({
  /**
   * 页面的初始数据
   */
  data: {
    region: ['上海市', '上海市', '黄浦区'],
    now_region: ['上海市', '上海市', '黄浦区'],
    formdata: '',  
    img_url:'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=501012038,2979999504&fm=26&gp=0.jpg',
    id:wx.getStorageSync('id')
  },
   //获取前台发送的数据
  formSubmit: function (e) {
    console.log(e)
    var warn = "";//弹框时提示的内容
    var flag = true;//判断信息输入是否完整
    if (e.detail.value.name == "") {
      warn = "请填写您的姓名！";
    } else if (e.detail.value.tel == "") {
      warn = "请填写您的手机号！";
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.phone))) {
      warn = "手机号格式不正确";
    } else if (e.detail.value.city == '') {
      warn = "请选择您的地址"
    } else if (e.detail.value.nowcity == "") {
      warn = "请输入您的现居地址";
    } else if (e.detail.value.work == '') {
      warn = "请输入您的职业";
    } else if (e.detail.value.sex == '') {
      warn = "请选择您的性别";
    } else if (e.detail.value.img_arr == '') {
      warn = "请上传图片";
    } else {
      flag = true
      adds = e.detail.value;
      this.upload()
    }
    //如果信息填写不完整，弹出输入框
    if (flag == true) {
      wx.showModal({
        title: '提示',
        content: warn
      })
    }
  },  
  //图片上传函数
  upload: function () {
    var that = this
    console.log('picker发送选择改变，携带值为',this.data.img_arr[0])
    for (var i = 0; i < this.data.img_arr.length; i++) { 
      wx.uploadFile({
        url: 'https://wx.gjzy.com/weixin/Admin/Index/jujia',
        filePath: that.data.img_arr[i],
        name: 'content',
        formData: adds,
        header: {
          "Content-Type": "multipart/form-data"
        },  
        success: function (res) {
          console.log(res)
          if (res.data == '200') {
            wx.showToast({
              title: '已提交发布！',
              duration: 3000,
              success:wx.navigateTo({
                url: '/pages/zl/zl',
              })
            });
          }else{
            wx.showToast({
              title: '失败请重试',
              duration: 3000,
              success:wx.redirectTo({
                url: '/pages/zl/zl',
              })
            });
          }
        }
      })
    }
    this.setData({
      formdata: ''
    })
  }, 
  // 选择图片上传函数
  upimg: function () {
    var that = this;
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {
          that.setData({
            img_arr:res.tempFilePaths
          })
        }
      })
  },
  //城市改变函数
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bindRegionChange_now: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      now_region: e.detail.value
    })
  },
  url2:function(){
    wx.redirectTo({
      url: '../zl/zl',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})